package com.aiju.cash.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.ShopApi;
import com.aiju.cash.ui.enter.util.EnterHelper;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.common.ImageLoader;
import com.aiju.utils.T;
import com.aiju.utils.Util;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.io.File;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-19 14:22
 * @version: V1.0
 * @Description: 店铺中心
 */
public class StoreActivity extends BaseTitleActivity implements View.OnClickListener {

    private final static int PHOTO_REQUEST = 1;
    private ImageView storePicImageView;
    private TextView storeNameTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        setCenterTitle("店铺中心");

        findViewById(R.id.ly_store_pic).setOnClickListener(this);
        findViewById(R.id.ly_store_name).setOnClickListener(this);
        findViewById(R.id.ly_store_qr).setOnClickListener(this);

        storeNameTextView = (TextView) findViewById(R.id.tv_store_name);
        storePicImageView = (ImageView) findViewById(R.id.iv_store_pic);

    }

    @Override
    protected void onResume() {
        storeNameTextView.setText(mUser.getShopName());
        if (!Util.isEmpty(mUser.getShopImageUrl())) {
            ImageLoader.loadImage(storePicImageView, mUser.getShopImageUrl());
        }
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ly_store_pic) {
           /* Intent intent = new Intent(StoreActivity.this, SelectActivity.class);
            startActivityForResult(intent, PHOTO_REQUEST);*/
            ImageDialogUtil.showSelectPicDialog(StoreActivity.this);
        } else if (v.getId() == R.id.ly_store_name) {
            Intent intent = new Intent(StoreActivity.this, StoreNameActivity.class);
            intent.putExtra("showName", mUser.getShopName());
            intent.putExtra("showType", StoreNameActivity.SHOW_STORE);
            startActivity(intent);
        } else if (v.getId() == R.id.ly_store_qr) {
            Intent intent = new Intent(StoreActivity.this, StoreCodeActivity.class);
            intent.putExtra("name", mUser.getShopName());
            startActivity(intent);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data) {
            return;
        }
        File logoFile = EnterHelper.onActivityResult(storePicImageView, requestCode, resultCode, data);
        if (logoFile!=null){
            mDialogHelper.showLoadingProgressDialog();
            new ShopApi().uploadShopImage(mUser.getToken(), logoFile, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    mDialogHelper.dismissProgressDialog();
                    T.ToastNetError();
                }

                @Override
                public void onResponse(String response, int id) {
                    mDialogHelper.dismissProgressDialog();
                    DataJson json = new DataJson(response);
                    if (json.isSuccess()) {
                        try {
                            String pic = json.getJsonObject().getString("data");
                            mUser.getShop().setImageUrl(pic);
                            if (!Util.isEmpty(mUser.getShop().getImageUrl())) {
                                ImageLoader.loadImage(storePicImageView, mUser.getShop().getImageUrl());
                            }
                            updateStore(pic);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        T.ToastShort(json.getMsg());
                    }
                }
            });
        }
    }
    private void updateStore(String url) {
        if (Util.isEmpty(url)) {
            return;
        }
        new ShopApi().updateShopImage(mUser.getShop().getId(), url, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson json = new DataJson(response);
                if (json.isSuccess()) {
                    T.ToastShort("店铺头像修改成功");
                } else {
                    T.ToastShort(json.getMsg());
                }
            }
        });
    }
}
